<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jul 14, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RCoreImporter::cls("html.AbstractHtmlCombobox" );

class HtmlSqlLevelCombobox extends AbstractHtmlCombobox {
	
	var $_sql;
	var $_parentField;
	var $_orderBy;
	
	var $_ignoreId = null;
	
	/**
	 * Constructor
	 *
	 * @param string $cbxName
	 * @param string $sql
	 * @param string - Name of parent Field $parentField
	 * @param array $valueOrArrayContain
	 */
	function __construct($cbxName, $sql, $parentField = 'parent_id', $valueOrArrayContain = null) {
		parent::__construct ( $cbxName, $valueOrArrayContain );
		$this->_sql = $sql;
		$this->_parentField = $parentField;
	}
	
	/**
	 * Set datainfo
	 *
	 * @param string $sql
	 * @param string $parentField
	 * @param string $orderBy
	 */
	function setInfo($sql, $parentField, $orderBy = '') {
		$this->_sql = $sql;
		$this->_parentField = $parentField;
		$this->_orderBy = $orderBy;
	}
	
	/**
	 * Get tree
	 *
	 * @param string $emptyText
	 * @param number $parentid
	 * @param string $space
	 * @param array $trees
	 * @return array trees
	 */
	function category_get_tree($emptyText = " - - Select Filter - - ", $parentid = null, $space = '--', $trees = NULL, $level = 0) {
		if (!$this->_isMultipleSelect && $emptyText !== null) {
			$trees [''] = $emptyText;
		}
		
		if (! $trees)
			$trees = array ();
		
		$where = array();	
		if ($parentid === null) {
			$where[] = "{$this->_parentField} IS NULL";
		}
		$where[] = "{$this->_parentField} = ".intval ( $parentid );
		
		$where	= " WHERE " .implode(' OR ',$where);
		
		$sql = SysDatabase::execute ( $this->_sql . $where );
		$level++;
		while ( $rs = mysql_fetch_array( $sql ) ) {
			if ($rs[0] == $this->_ignoreId) {
				continue;	
			}
			if (! array_key_exists ( $rs [0], $trees )) {
				$trees ["{$rs [0]}"] = "<span class=\"level_{$level}\">{$space} {$rs[1]}</span>";
			}
			
			$trees = HtmlSqlLevelCombobox::category_get_tree ( $emptyText, $rs [0], $space . '--', $trees, $level );
		}
		return $trees;
	}
	
	/**
	 * Set ignore id
	 *
	 * @param int $ignoreId
	 */
	function setIgnoreId($ignoreId) {
		$this->_ignoreId = $ignoreId;
	}
	
	/**
	 * Create Combobox
	 *
	 * @param string $emptyText
	 * @param string $space
	 * @param string $cssCls
	 * @param string $style
	 * @param number $id
	 * @param string $extAttributes
	 * @return HTML to show combobox
	 */
	function createCombobox($emptyText = " - - Select Filter - - ", $space = " -- ", $cssCls = null, $style = null, $id = null, $extAttributes = "") {
		RCoreImporter::cls("helper.RHtmlHelper" );
		if (!$this->_arrayData) {
			$this->_arrayData = HtmlSqlLevelCombobox::category_get_tree ( $emptyText );
		}
		return parent::createCombobox($emptyText, $cssCls, $sType, $id, $extAttributes);
	}
}

?>